package net.eearley.redpoint.app;

import java.util.List;

import net.eearley.redpoint.app.base.IConfigurator;
import net.eearley.redpoint.app.utils.Util;

/**
 * The Class App.
 */
public class App {

	/** The configuration. */
	private IConfigurator configuration;

	/** The validator. */
	private Validator validator;

	/** The inventory. */
	private Inventory inventory;

	/** The horse list. */
	private List<Horse> horseList;

	/** The atm. */
	private ATM atm;

	/**
	 * Instantiates a new app.
	 * 
	 * @param validator
	 *            the validator
	 * @param configurator
	 *            the configurator
	 * @param atm
	 *            the atm
	 */
	public App(Validator validator, IConfigurator configurator, ATM atm) {
		this.configuration = configurator;
		this.validator = validator;
		this.atm = atm;
		setInventory(configurator.getInitialDenoms());
		setHorseList(configurator.getHorses());

	}

	/**
	 * Gets the atm.
	 * 
	 * @return the atm
	 */
	public ATM getAtm() {
		return atm;
	}

	/**
	 * Gets the configuration.
	 * 
	 * @return the configuration
	 */
	public IConfigurator getConfiguration() {
		return configuration;
	}

	/**
	 * Gets the horse.
	 * 
	 * @param horseId
	 *            the horse id
	 * 
	 * @return the horse
	 */
	public Horse getHorse(Integer horseId) {
		for (Horse horse : horseList) {
			if (horse.getNumber() == horseId)
				return horse;

		}
		return null;
	}

	/**
	 * Gets the horse list.
	 * 
	 * @return the horse list
	 */
	public List<Horse> getHorseList() {
		return horseList;
	}

	/**
	 * Gets the inventory.
	 * 
	 * @return the inventory
	 */
	public Inventory getInventory() {
		return inventory;
	}

	/**
	 * Gets the validator.
	 * 
	 * @return the validator
	 */
	public Validator getValidator() {
		return validator;
	}

	/**
	 * Gets the winning horse.
	 * 
	 * @return the winning horse
	 */
	public Horse getWinningHorse() {
		for (Horse horse : horseList) {
			if (horse.isDidWin())
				return horse;
		}
		return null;
	}

	/**
	 * Sets the atm.
	 * 
	 * @param atm
	 *            the new atm
	 */
	public void setAtm(ATM atm) {
		this.atm = atm;
	}

	/**
	 * Sets the configuration.
	 * 
	 * @param configuration
	 *            the new configuration
	 */
	public void setConfiguration(IConfigurator configuration) {
		this.configuration = configuration;
	}

	/**
	 * Sets the horse list.
	 * 
	 * @param horseList
	 *            the new horse list
	 */
	public void setHorseList(List<Horse> horseList) {
		this.horseList = horseList;
	}

	/**
	 * Sets the inventory.
	 * 
	 * @param inventory
	 *            the new inventory
	 */
	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}

	/**
	 * Sets the validator.
	 * 
	 * @param validator
	 *            the new validator
	 */
	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	/**
	 * Sets the winning horse.
	 * 
	 * @param horseId
	 *            the new winning horse
	 */
	public void setWinningHorse(Integer horseId) {
		for (Horse horse : horseList) {
			if (horse.getNumber() == horseId)
				horse.setDidWin(true);
			else
				horse.setDidWin(false);
		}

	}

	/**
	 * Calculate winnings.
	 * 
	 * @param playerHorse
	 *            the player horse
	 * @param winningHorse
	 *            the winning horse
	 * @param betAmount
	 *            the bet amount
	 * 
	 * @return the integer
	 */
	public Integer calculateWinnings(Horse playerHorse, Horse winningHorse,
			Integer betAmount) {
		Integer returnOnBet = 0;
		if (playerHorse.equals(winningHorse)) {
			returnOnBet = betAmount * playerHorse.getOdds();
			return returnOnBet;
		} else {
			return returnOnBet;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Inventory:");
		sb.append('\n');
		sb.append(inventory);
		sb.append("Horses:");
		sb.append('\n');
		sb.append(Util.listToString(horseList));
		return sb.toString();
	}

}
